<?php

// connection methods
function Connection(){
    $link= mysql_connect("127.0.0.1","efkhoury_aok","!NE@V)KUOtWe") or die("Cannot connect to data base server");

    $connect=mysql_select_db("efkhoury_aok",$link) or die("cannot connect to efkhoury_aok database");
}

//UpdateLocation(email,latitude,longitude)
function UpdateLocation($email, $latitude, $longitude){
    // connect to db
    Connection();

	$date = date('Y-m-d H:i:s', time());
	
    // sql query , update location set lat and long in record that have the matching email
    $sql="UPDATE `findfriends`.`people` SET 
		`latitude` = '".$latitude."',
		`longitude` = '".$longitude."',
		`lastseen` = '".$date."'
		WHERE `people`.`email` ='".$email."'" ;

    // query execution
	$result=mysql_query($sql) or print(0);//die("cannot execute query:  ".$sql);
	
	echo mysql_affected_rows();
}

//SignUp(email,name)
function SignUp($email,$name)
{
    // connect to db
    Connection();
    
    // insert query
    $sql="INSERT INTO `findfriends`.`people` (`name`, `email`, `latitude`, `longitude`, `lastseen`)
		VALUES ('".$name."', '".$email."', 0 , 0, '2001-08-28 00:00:00');";

	// query execution
	$result=mysql_query($sql) or print(-1);//die("cannot execute query:  ".$sql) ;
	
	if(mysql_affected_rows() == 1) {
		$sql = "SELECT ID FROM people WHERE email='".$email."';";
		$result=mysql_query($sql) or print(-1);
		
		while($row = mysql_fetch_object($result)) {
			echo $row->ID;
			break;
		}
	}
}

/*
//UpdateProfile(email,name)
function UpdateProfile($email,$name)
{
    // connect to db
    Connection();

    // insert query
    $sql="UPDATE `findfriends`.`people` SET
		`name` = '".$name."',
		`email` = '".$email."',
		`picture` ='".$pic."'
		WHERE `people`.`email` ='".$email."'";

	// query execution
	$result=mysql_query($sql) or die("cannot execute query:  ".$sql) ;
}
*/

// GetPeople(latitude,longitude,radius)
function GetPeople($latitiude, $longitude, $radius)
{
    // connect to db
    Connection();

    // insert query
    $sql="SELECT * FROM `people` WHERE `latitude`>=".($latitiude-$radius)." and `latitude`<=".($latitiude+$radius)."
        and  `longitude`>=".($latitiude-$radius)." and `longitude`<=".($longitude+$radius)."";
	
	// query execution
	$result=mysql_query($sql) or die("cannot execute query:  ".$sql) ;
	$array = NULL;

	if (mysql_num_rows($result))
	{
		while($post=  mysql_fetch_assoc($result))
		{
			$array=array($post);
		}
	}

	header('Content-type: application/json');
	echo json_encode($array);
}

// GetAllPeople()
function GetAllPeople()
{
    // connect to db
    Connection();

    // insert query
    $sql="SELECT * FROM `people`";
	
	// query execution
	$result=mysql_query($sql) or die("cannot execute query:  ".$sql) ;
	$array = array();

	if (mysql_num_rows($result))
	{
		while($post=mysql_fetch_assoc($result))
		{
			$array[]=$post;
		}
	}

	header('Content-type: application/json');
	echo json_encode($array);
}

?>